# coding=utf-8
import time

import allure
import jsonpath

from common.assert_operator import *
from common.db import *
from common.read_yaml import *
from common.utils import Utils

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
execute_sql = sql() # sql集合
utils = Utils()
data = ReadYaml_By_Root_Dict("case/99_outside_api_test/01_workflow_outside_test/99_01_6_GetBusinessTypeList_SaveDraft_DeleteDraft.yml").get_yaml_data()  # 读取数据

@allure.feature('测试工作流外部接口：获取业务类型，保存草稿，删除草稿')  # 测试报告显示测试功能
class Test_99_01_6_GetBusinessTypeList_SaveDraft_DeleteDraft():
    """
    测试接口:
    /wf/api/businesstype/GetBusinessTypeList
    /wf/api/ProcessEntiy/SaveDraft
    /wf/api/ProcessEntiy/DeleteDraft
    """
    # 获取yml文件中[获取业务类型]的数据参数
    Params = data['获取业务类型']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_GetBusinessTypeList(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        user_guid = utils.get_user_guid_by_code(param['用户'])    #获取用户guid
        resp = main.GetBusinessTypeList(param['请求体'],user_guid)   #发起请求1
        contract_format = Matcher(
            {
                "code": 0,
                "data": EachLike({
                    "BusinessTypeGUID": Term(r"(\w+)-(\w+)-(\w+)-(\w+)-(\w+)",
                                             example="f97b340b-085f-587d-5874-39fdf94cd978"),
                    "BusinessTypeName": Term(r"^.{3,20}$",example="供应商招募审批"),# 3-20位任意字符
                    "Application": Term(r"^\d{4,}$",example="0220"),    # 4位以上数字
                    "HierarchyCode": Term(r"^\d{4,}\..{3,50}$",example="0220.供应商招募审批")}),   #4位以上数字开头，.加3-20位任意字符结尾。
                "errorinfo":"",
                "message": ""
            })
        pact.pact_verify(contract_format,resp.json())

    # 获取yml文件中[保存草稿]的数据参数
    Params = data['保存草稿']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_SaveDraft(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        user_guid = utils.get_user_guid_by_code(param['用户'])    #获取用户guid
        resp = main.SaveDraft(param['请求体'],user_guid)   #发起请求
        dict_assert(expect['响应校验'],resp.json())    # 结果断言


    # 获取yml文件中[保存草稿_异常场景]的数据参数
    Params = data['保存草稿_异常场景']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_SaveDraft_Abnormal(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        user_guid = utils.get_user_guid_by_code(param['用户'])    #获取用户guid
        resp = main.SaveDraft(param['请求体'],user_guid)   #发起请求
        dict_assert(expect['响应校验'],resp.json())    # 结果

    # 获取yml文件中[删除草稿]的数据参数
    Params = data['删除草稿']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_DeleteDraft(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        user_guid = utils.get_user_guid_by_code(param['用户'])    #获取用户guid
        resp = main.DeleteDraft(param['请求体'],user_guid)   #发起请求
        dict_assert(expect['响应校验'],resp.json())    # 结果断言

    # 获取yml文件中[删除草稿_异常场景]的数据参数
    Params = data['删除草稿_异常场景']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_DeleteDraft_Abnormal(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        user_guid = utils.get_user_guid_by_code(param['用户'])    #获取用户guid
        resp = main.DeleteDraft(param['请求体'],user_guid)   #发起请求
        dict_assert(expect['响应校验'],resp.json())    # 结果断言
